package com.zsrd.lessons.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsrd.core.response.ResponseSupport;
import com.zsrd.lessons.domain.TsQuestionBank;
import com.zsrd.lessons.domain.TsSubject;
import com.zsrd.lessons.domain.dto.TsQuestionAddDto;
import com.zsrd.lessons.domain.dto.TsQuestionUpdateDto;
import com.zsrd.lessons.domain.vo.TsQuestionBankVo;
import com.zsrd.lessons.domain.vo.TsQuestionVo;
import com.zsrd.lessons.service.TsQuestionBankService;
import com.zsrd.lessons.service.TsQuestionService;
import com.zsrd.lessons.service.TsSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题目表 前端控制器
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private TsQuestionBankService tsQuestionBankService;
    @Autowired
    private TsQuestionService tsQuestionService;
    @Autowired
    private TsSubjectService tsSubjectService;


    /**
     * 查看题目结果答案
     *
     * @param id 题目id
     * @return
     */
    @RequestMapping("/lookAnswer")
    public ResponseSupport lookAnswer(Long id) {
        try {
            Map<String, Object> m = tsQuestionService.lookAnswer(id);
            return ResponseSupport.success(m);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 题目列表
     *
     * @param orgNo
     * @param loginId
     * @return
     */
    @RequestMapping("/getQuestionList")
    public ResponseSupport getQuestionList(@RequestParam("orgNo") String orgNo,
                             @RequestParam("loginId") String loginId,
                             @RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam("current") Long current,
                             @RequestParam("size") Long size) {
        try {
            IPage<TsQuestionVo> page = tsQuestionService.tsQuestionService(orgNo, loginId, keyword, current, size);
            return ResponseSupport.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 添加题目
     *
     * @return
     */
    @RequestMapping("/addQuestion")
//    public R addQuestion(@RequestBody @Valid TsQuestionAddDto a) {
    public ResponseSupport addQuestion(@RequestBody TsQuestionAddDto a) {
        try {
            tsQuestionService.addQuestion(a);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 编辑题目
     *
     * @return
     */
    @RequestMapping("/updateQuestion")
//    public R updateQuestion(@RequestBody @Valid TsQuestionUpdateDto a) {
    public ResponseSupport updateQuestion(@RequestBody TsQuestionUpdateDto a) {
        try {
            tsQuestionService.updateQuestion(a);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 编辑题目
     *
     * @return
     */
    @RequestMapping("/deleteQuestion")
    public ResponseSupport deleteQuestion(@RequestParam Long id) {
        try {
            tsQuestionService.deleteQuestion(id);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }


    /**
     * 添加题库
     *
     * @param orgNo
     * @param title
     * @return
     */
    @RequestMapping("/addQuestionBank")
    public ResponseSupport addQuestionBank(@RequestParam("orgNo") String orgNo, @RequestParam("title") String title) {
        try {
            tsQuestionBankService.addQuestionBank(orgNo, title);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 编辑题库
     *
     * @param id
     * @param orgNo
     * @param title
     * @return
     */
    @RequestMapping("/updateQuestionBank")
    public ResponseSupport updateQuestionBank(@RequestParam("id") Long id, @RequestParam("orgNo") String orgNo, @RequestParam("title") String title) {
        try {
            tsQuestionBankService.updateQuestionBank(id, orgNo, title);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 删除题库
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteQuestionBank")
    public ResponseSupport deleteQuestionBank(@RequestParam("id") Long id) {
        try {
            tsQuestionBankService.deleteQuestionBank(id);
            return ResponseSupport.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }


    /**
     * 题库列表
     *
     * @param orgNo
     * @param loginId
     * @return
     */
    @RequestMapping("/getQuestionBankList")
    public ResponseSupport getQuestionBankList(@RequestParam("orgNo") String orgNo,
                                 @RequestParam("loginId") String loginId,
                                 @RequestParam("current") Long current,
                                 @RequestParam("size") Long size) {
        try {
            IPage<TsQuestionBankVo> page = tsQuestionBankService.getQuestionBankList(orgNo, loginId, current, size);
            return ResponseSupport.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 题库列表 获取所有的   不分页
     *
     * @param orgNo
     * @return
     */
    @RequestMapping("/getQuestionBankAllList")
    public ResponseSupport getQuestionBankAllList(@RequestParam("orgNo") String orgNo) {
        try {
            List<TsQuestionBank> page = tsQuestionBankService.getQuestionBankAllList(orgNo);
            return ResponseSupport.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    @RequestMapping("/questionDetail")
    public ResponseSupport questionDetail(Long id) {
        try {
            Map<String, Object> d = tsQuestionService.questionDetail(id);
            return ResponseSupport.success(d);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    @RequestMapping(value="/getAllSubjects",method = RequestMethod.POST)
    public ResponseSupport getAllSubjects() {

        try{
            List<TsSubject> list = tsSubjectService.getAllSubjectList();
            return ResponseSupport.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }

    }

}
